# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import os,base64
from django.shortcuts import render
from django.http import HttpResponse,HttpResponseRedirect
from .models import Img,VR
from django.contrib.auth.models import User
import datetime
# Create your views here.

def welcome(request):#图像1
    BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    files = request.FILES.getlist('myFile')
    ss = ""
    for f in files:
        destination = open(BASE_DIR + '\\media\\IMG\\a2\\' + f.name, 'wb+')  # 上传的文件都放到/media/photos/文件夹里
        ss = ss + ',' + f.name

        for chunk in f.chunks():
            destination.write(chunk)
        destination.close()

    ss1 = ss[1:]
    print ss1
    img = Img(img_url=ss1)
    img.save()
    return render(request, 'show.html')

def welcome1(request,cc):#修改VR数据
    if request.user.is_authenticated:
        ooo = request.session.get(u'_auth_user_id', None)
        aa = User.objects.get(id=ooo)
        ss = aa.username
        uu = VR.objects.get(id=cc)
        context = {'name': ss, 'url':uu.ico,'bt':uu.bt,'url12':uu.url12,'zb':uu.zb}
        if request.method == 'POST':
            uu.bt =request.POST.get("bt")

            if request.FILES.get('img'):
                uu.ico =request.FILES.get('img')
            uu.zb=request.POST.get("zb")
            uu.url12=request.POST.get("url")
            uu.save()
            #return HttpResponseRedirect(u'admin/admin/app/app/vr/{0}/change/'.format(cc))
            return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
        return render(request, 'admin/app/vr/change_form.html', context)
    else:
        return HttpResponseRedirect('/admin/app/dd/login/')

    return render(request, 'admin/app/vr/change_form.html', context)
   # return render(request, 'admin/app/vr/add.html')

def vr(request):#VR增加
    if request.user.is_authenticated:
        ooo = request.session.get(u'_auth_user_id', None)
        aa = User.objects.get(id=ooo)
        ss = aa.username
        context = {'name': ss}
        if request.method == 'POST':
            bt =request.POST.get("bt")
            ico = request.FILES.get('img')
            zb=request.POST.get("zb")
            url12=request.POST.get("url")
            ims=VR(bt=bt,ico=ico,zb=zb,url12=url12)
            ims.save()
        return render(request, 'admin/app/vr/add.html',context)
    else:
        return HttpResponseRedirect('/admin/app/login/')
    return render(request, 'admin/app/vr/add.html',context)


def map(request):
    if request.method == 'POST':
        img = Img(img_url=request.FILES.get('img'))
        img.save()
    return render(request, 'mq/index.html')

def uploadImg(request):
    BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    files = request.FILES.getlist('img')
    ss = ""
    for f in files:
        destination = open(BASE_DIR + '/media/IMG/' + f.name,'wb+')      #上传的文件都放到/media/photos/文件夹里
        ss=ss+','+f.name

        for chunk in f.chunks():
            destination.write(chunk)
        destination.close()

    ss1=ss[1:]
    print ss1
    img = Img(img_url=ss1)
    img.save()

    return render(request, 'imgUpload.html')
def data1(request):
    if request.method == 'POST':
        fomrmat = request.POST.get("imgFormat")
        user1 = request.POST.get("imgBase")

        u2 = request.POST.get("lookIndex")
        now = datetime.datetime.now()
        time_str3 = now.strftime('%Y%m%d%H%M%S')
        file_address = time_str3+u2+ r"."+fomrmat
        all1=user1.split(',')

        ss1=all1[1]
        imgdata = base64.b64decode(ss1)
        file = open(file_address, 'wb')
        file.write(imgdata)
        file.close()
        return HttpResponse("上传成2功")
    else:
         print 'abc'
         return HttpResponse('操作失败')
